/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.ListIterator;

import javax.swing.JComboBox;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.analysismodel.AnalysisModel;
import br.ufmg.dees.insane.model.disc.analysismodel.AxiSymetricAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.Beam;
import br.ufmg.dees.insane.model.disc.analysismodel.Grid;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneFrame;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneStrainAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneStressAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneTruss;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.command.XY2XZCommand;
import br.ufmg.dees.insane.ui.rich.full.command.XZ2XYCommand;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public class GlobalAnalysisModelDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	public AnalysisModel anl;
	private String[] analysisTypeStr = new String[7];
	private JComboBox analysisType ;
	private Command cmd;
	
//*****************************************************************************
	
	public GlobalAnalysisModelDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		JPanel mainPane = new JPanel();
		
		analysisTypeStr[0] = parent.getResourceBundle().getString("dGAnlDialog01");
		analysisTypeStr[1] = parent.getResourceBundle().getString("dGAnlDialog02");
		analysisTypeStr[2] = parent.getResourceBundle().getString("dGAnlDialog03");
		analysisTypeStr[3] = parent.getResourceBundle().getString("dGAnlDialog04");
		analysisTypeStr[4] = parent.getResourceBundle().getString("dGAnlDialog07");
		analysisTypeStr[5] = parent.getResourceBundle().getString("dGAnlDialog08");
		analysisTypeStr[6] = parent.getResourceBundle().getString("dGAnlDialog09");
		
		mainPane.setPreferredSize(new Dimension(200,35));
		mainPane.setSize(new Dimension(200,35));
		
		mainPane.setLayout(new GridLayout(1,1,2,2));
		
		analysisType = new JComboBox(analysisTypeStr);
		analysisType.setPreferredSize(new Dimension (180,26));
		
		JPanel subPane = new JPanel();
		subPane.add(analysisType);
		mainPane.add(subPane);
		
		setPane(mainPane);
		String str;
		try {
			str = parent.getModel().getDiscreteModel().getGlobalAnalysisModel().getType();
		} catch (Exception e) {
			str = "PlaneFrame";
		}
		if (str.equals("Beam"))
			analysisType.setSelectedIndex(0);
		else if (str.equals("PlaneFrame"))
			analysisType.setSelectedIndex(1);
		else if (str.equals("PlaneTruss"))
			analysisType.setSelectedIndex(2);
		else if (str.equals("Grid"))
			analysisType.setSelectedIndex(3);
		else if (str.equals("PlaneStress"))
			analysisType.setSelectedIndex(4);
		else if (str.equals("PlaneStrain"))
			analysisType.setSelectedIndex(5);
		else if (str.equals("Axisymmetric"))
			analysisType.setSelectedIndex(6);
	}
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(this.parent, parent.getResourceBundle().getString("dGAnlDialog05"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		try {
			
			FemModel fmodel = parent.getModel().getDiscreteModel();
			String actualGAnl = fmodel.getGlobalAnalysisModel().getType();
			
			if (analysisType.getSelectedIndex() == 0) {
				anl = new Beam();
				if (actualGAnl.equals("Grid")) {
					cmd = new XZ2XYCommand(parent);
					cmd.execute();
				}
			}
			else if (analysisType.getSelectedIndex() == 1) {
				anl = new PlaneFrame();
				if (actualGAnl.equals("Grid")) {
					cmd = new XZ2XYCommand(parent);
					cmd.execute();
				}
			}
			else if (analysisType.getSelectedIndex() == 2) {
				anl = new PlaneTruss();
				if (actualGAnl.equals("Grid")) {
					cmd = new XZ2XYCommand(parent);
					cmd.execute();
				}
			}
			else if (analysisType.getSelectedIndex() == 3) {
				anl = new Grid();
				if (!(actualGAnl.equals("Grid"))) {
					cmd = new XY2XZCommand(parent);
					cmd.execute();
				}
			}
			else if (analysisType.getSelectedIndex() == 4) {
				anl = new PlaneStressAnalysisM();
				if (!(actualGAnl.equals("Grid"))) {
					cmd = new XY2XZCommand(parent);
					cmd.execute();
				}
			}
			else if (analysisType.getSelectedIndex() == 5) {
				anl = new PlaneStrainAnalysisM();
				if (!(actualGAnl.equals("Grid"))) {
					cmd = new XY2XZCommand(parent);
					cmd.execute();
				}
			}
			else if (analysisType.getSelectedIndex() == 6) {
				anl = new AxiSymetricAnalysisM();
				if (!(actualGAnl.equals("Grid"))) {
					cmd = new XY2XZCommand(parent);
					cmd.execute();
				}
			}
			boolean existentAnl = false;
			
			ListIterator anls = fmodel.getAnalysisModelsList().listIterator();
			while (anls.hasNext()) {
				AnalysisModel anl2 = (AnalysisModel) anls.next();
				if (anl2.getType().equals(anl.getType()))
					existentAnl = true;
			}
			
			if (!existentAnl) {
				fmodel.add(anl);
				fmodel.setGlobalAnalysisModel(anl);
			} else {
				fmodel.setGlobalAnalysisModel(fmodel.getAnalysisModel(anl.getType()));
			}
			
			ListIterator elms = fmodel.getElementsList().listIterator();
			System.out.println(fmodel.getElementsList().size());
			while (elms.hasNext()) {
			    Element elm = (Element) elms.next();
				elm.setAnalysisModel(anl);
				if(elm instanceof FrameElement) {
                       FrameElement felm = (FrameElement) elm;
                       felm.setShape(fmodel); 
                }
			}
			
			if (!(anl.getType().equals(actualGAnl))){
				parent.cleanResults();
			}
			
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	
	}
	
//*****************************************************************************
}
